unit u_sound;

interface
{$O-}
uses
  system.Windows.forms,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectSound,
 Microsoft.DirectX.AudioVideoPlayback;

type
  Sound = Class
    _device : Device;
    _buffer : SecondaryBuffer;
    _BufferPrinc : Buffer;
    _desc : BufferDescription;
    _mp3 : Audio;
    procedure createdevice (formctrl : control);
    procedure playsound (son : string);
    procedure playsoundLoop (son : string);
    procedure playmp3 (son : string);
    procedure stopsound(formctrl : control);
    procedure Dispose;
  end;

implementation
procedure Sound.createdevice (formctrl : control);
begin
  _desc := BufferDescription.Create();
  _desc.GlobalFocus := true;
  _desc.ControlVolume := true;
  _device := device.Create();
  _device.SetCooperativeLevel(formctrl,CooperativeLevel.Priority);
end;

procedure Sound.playsound (son : string);
  begin
    _Buffer := SecondaryBuffer.create('./Sound/'+son, _device);
    _Buffer.Play(0, BufferPlayFlags.Default);
  end;

procedure Sound.playsoundLoop (son : string);
  begin
    _BufferPrinc := Secondarybuffer.Create('./Sound/'+son, _desc, _device);
    _BufferPrinc.Play(0, BufferPlayFlags.Looping);
  end;

procedure Sound.playmp3(son: string);
begin
  _mp3 := Audio.Create('./Sound/'+son);
  _mp3.Play();
end;

procedure Sound.stopsound(formctrl : control);
  begin
    _buffer.Stop;
    _mp3.Stop;
  end;

procedure Sound.Dispose;
begin
  _device.dispose;
end;
end.
